package com.my.zidingyikongjian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import com.my.zidingyikongjian.R;
import com.my.zidingyikongjian.view.one.ArcView;
import com.my.zidingyikongjian.view.one.MoreLineView;
import com.my.zidingyikongjian.view.one.MorePoints;
import com.my.zidingyikongjian.view.one.OvalView;
import com.my.zidingyikongjian.view.one.RectView;
import com.my.zidingyikongjian.view.one.RemoveLine;
import com.my.zidingyikongjian.view.one.ReoundRectView;
import com.my.zidingyikongjian.view.one.SingleLineView;
import com.my.zidingyikongjian.view.one.PointView;

public class DrawLineActivity extends AppCompatActivity implements View.OnClickListener {
    private int flag = -1;
    /**
     * 屏幕宽度
     */
    private float dx;
    /**
     * 屏幕高度
     */
    private float dy;
    /**
     * 移动线的宽度
     */
    private float moveLineMaxWidth;
    /**
     * 线的起始位置 X
     */
    private float moveLineStartPositionX;
    /**
     * 线的起始位置 Y
     */
    private float moveLineStartPositionY;
    private FrameLayout flLine;
    /**
     * 可动的线
     */
    private RemoveLine removeLine;
    private float startX, endX;
    /**
     * 移动
     */
    private Button btnLeft, btnRight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_draw_line);

        Intent dataIntent = getIntent();
        flag = Integer.valueOf(dataIntent.getStringExtra("draw_line"));
        flLine = findViewById(R.id.fl_line);

        if (flag != -1) {
            if (flag == 0) { // 画单条线
                flLine.addView(new SingleLineView(this));
            } else if (flag == 1) { // 画多条线
                flLine.addView(new MoreLineView(this));
            } else if (flag == 2) {
                goMoveLine();
            } else if (flag == 3) { // 画点
                flLine.addView(new PointView(this));

            } else if (flag == 4) { // 画多个点
                flLine.addView(new MorePoints(this));
            } else if (flag == 5) { // 画矩形
                flLine.addView(new RectView(this));
            } else if (flag == 6) { // 画圆角矩形
                flLine.addView(new ReoundRectView(this));
            } else if (flag == 7) { // 画椭圆
                flLine.addView(new OvalView(this));
            } else if (flag == 8) { // 画弧
                flLine.addView(new ArcView(this));
            }
        }

        btnLeft = findViewById(R.id.btn_left_line);
        btnLeft.setOnClickListener(this);
        btnRight = findViewById(R.id.btn_right_line);
        btnRight.setOnClickListener(this);
    }

    private void goMoveLine() {
        // 1. 获取屏幕的宽度，高度
        DisplayMetrics dm = getResources().getDisplayMetrics();
        dx = dm.heightPixels;
        dy = dm.widthPixels;
        //2.设置线的长度(为屏幕的1/10)
        moveLineMaxWidth = dx / 10;
        //3.设置 线的起点位置
        //3.1 默认X轴的位置固定为0,默认Y轴的位置固定为100；
        startX = 0;
        endX = startX + moveLineMaxWidth;
        dy = 100;

        removeLine = new RemoveLine(this, startX, dy, endX, dy);
        flLine.addView(removeLine);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_right_line: // 右移
                goRight();
                break;
            case R.id.btn_left_line: // 左移
                goLeft();
                break;
        }
    }

    /**
     * 右移
     */
    private void goRight() {
        startX = endX;
        endX = endX + moveLineMaxWidth;
        removeLine = new RemoveLine(this, startX, dy, endX, dy);
        flLine.addView(removeLine);
    }

    /**
     * 左移
     */
    private void goLeft() {
        endX = startX;
        startX -= moveLineMaxWidth;
        removeLine = new RemoveLine(this, startX, dy, endX, dy);
        flLine.addView(removeLine);
    }
}
